<?php
/**
 * @package site
 */
class PackagePrice extends DataObject implements PermissionProvider {
    private static $singular_name = "Package Price";
    private static $plural_name = "Package Prices";

    private static $db = array(
        'Price' => 'Currency',
        'BV' => 'Int'
    );

    private static $belongs_many_many = array('Packages' => 'Package');
	
	private static $default_records = array(
		array(
			'Price' => 1000, 
			'BV' => 800
		),
		array(
			'Price' => 3000, 
			'BV' => 2400
		),
		array(
			'Price' => 5000, 
			'BV' => 4000
		),
		array(
			'Price' => 10000, 
			'BV' => 8000
		),
		array(
			'Price' => 30000, 
			'BV' => 24000
		),
		array(
			'Price' => 50000, 
			'BV' => 40000
		)
	);

    private static $default_sort = "Price";

    private static $searchable_fields = array(
        'Price' => array(
            'filter' => 'GreaterThanOrEqualFilter'
        ),
        'BV' => array(
            'filter' => 'GreaterThanOrEqualFilter'
        )
    );

    private static $summary_fields = array(
        'Price',
        'BV'
    );

    public function fieldLabels($includerelations = true) {
        $labels = parent::fieldLabels($includerelations);

        $labels['Price'] = _t('PackagePrice.PRICE', 'Price');
        $labels['BV'] = _t('PackagePrice.BV', 'BV');
        $labels['Packages'] = _t('PackagePrice.PACKAGE', 'Package');
        $labels['PackagesText'] = _t('PackagePrice.PACKAGE', 'Package');

        return $labels;
    }

    function PackagesText() {
        return implode(', ', $this->Packages()->map()->toArray());
    }

    function canView($member = false) {
        $extended = $this->extendedCan(__FUNCTION__, $member);
        if($extended !== null) {
            return $extended;
        }
        return Permission::check('VIEW_PackagePrice');
    }

    function canEdit($member = false) {
    	if(!$this->exists() && $this->canCreate($member)){
    		return true;
    	}
        $extended = $this->extendedCan(__FUNCTION__, $member);
        if($extended !== null) {
            return $extended;
        }
        return Permission::check('EDIT_PackagePrice');
    }

    function canDelete($member = false) {
        $extended = $this->extendedCan(__FUNCTION__, $member);
        if($extended !== null) {
            return $extended;
        }
        return Permission::check('DELETE_PackagePrice');
    }

    function canCreate($member = false) {
        $extended = $this->extendedCan(__FUNCTION__, $member);
        if($extended !== null) {
            return $extended;
        }
        return Permission::check('CREATE_PackagePrice');
    }

    public function providePermissions() {
        return array(
            'VIEW_PackagePrice' => array(
                'name' => _t('PackagePrice.PERMISSION_VIEW', 'Allow view access right'),
                'category' => _t('PackagePrice.PERMISSIONS_CATEGORY', 'Package Price')
            ),
            'EDIT_PackagePrice' => array(
                'name' => _t('PackagePrice.PERMISSION_EDIT', 'Allow edit access right'),
                'category' => _t('PackagePrice.PERMISSIONS_CATEGORY', 'Package Price')
            ),
            'DELETE_PackagePrice' => array(
                'name' => _t('PackagePrice.PERMISSION_DELETE', 'Allow delete access right'),
                'category' => _t('PackagePrice.PERMISSIONS_CATEGORY', 'Package Price')
            ),
            'CREATE_PackagePrice' => array(
                'name' => _t('PackagePrice.PERMISSION_CREATE', 'Allow create access right'),
                'category' => _t('PackagePrice.PERMISSIONS_CATEGORY', 'Package Price')
            )
        );
    }

}
?>
